package org.shj.spark.operator;

import java.util.Arrays;
import java.util.List;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;

import scala.Tuple2;

public class GroupByKeyOperator {

	public static void main(String[] args) {
		SparkConf conf = new SparkConf().setMaster("local").setAppName("GroupByKeyOperator");
		JavaSparkContext sc = new JavaSparkContext(conf);
		
		List<Tuple2<String, Integer>> scoreList = Arrays.asList(new Tuple2<String, Integer>("xuruyun", 100), 
				new Tuple2<String, Integer>("liangyongqi", 150),
				new Tuple2<String, Integer>("zhuyin", 160),
				new Tuple2<String, Integer>("zhuyin", 180));
		
		JavaPairRDD<String, Integer> rdd = sc.parallelizePairs(scoreList);
		rdd.groupByKey().foreach(new VoidFunction<Tuple2<String, Iterable<Integer>>>(){
			private static final long serialVersionUID = 2000922712443145320L;

			@Override
			public void call(Tuple2<String, Iterable<Integer>> tuple) throws Exception {
				System.out.println(tuple._1 + " " + tuple._2);
				
			}
			
		});
		
		sc.close();
	}

}
